library(Hmisc)

plotdata <- 
  analysis_df %>%
  group_by(YEAR, city) %>%
  filter(!is.na(treat)) %>%
  summarise(govt_total = sum(num[industry_recode=='local']),
            irish_total = sum(num[birthplace_recode_foreign=='irish']),
            pop_total = sum(num)) %>%
  ungroup() %>%
  left_join(analysis_df_3 %>%
              filter(YEAR==1880) %>%
              group_by(city, govt_n_total) %>%
              summarise() %>%
              rename(size_1880 = govt_n_total)) %>%
  left_join(analysis_df_3 %>%
              group_by(city) %>%
              mutate(max_year = max(YEAR[treat==0])) %>%
              filter(YEAR==max_year) %>%
              group_by(city, govt_n_total) %>%
              summarise() %>%
              rename(size_last = govt_n_total)) %>%
  ungroup() %>%
  mutate(quintile_1880 = cut2(size_1880, g = 5),
         quintile_last = cut2(size_last, g = 5)) %>%
  filter(!is.na(quintile_1880))
  

descriptive_plot <-
  plotdata %>%
  group_by(YEAR, quintile_last) %>%
  summarise(pct_irish = sum(irish_total)/sum(pop_total)) %>%
  filter(!is.na(quintile_last)) %>%
  ggplot(aes(x=YEAR, y = pct_irish*100, linetype = as.factor(quintile_last), shape = as.factor(quintile_last))) +
  geom_line() + 
  geom_point() + 
  theme_bw() +
  labs(y='Percent Irish', x = '', linetype = 'Govt employees\n(pre-reform)', shape = 'Govt employees\n(pre-reform)') + 
  theme(panel.grid.minor = element_blank(), panel.grid.major.x = element_blank(), panel.grid.major.y = element_line(size = .2)) + 
  theme(strip.background =element_rect(fill="white")) +
  theme(panel.border = element_blank(),
        axis.line.x = element_line(size = 0.5, linetype = "solid", colour = "black"),
        axis.line.y = element_line(size = 0.5, linetype = "solid", colour = "black"))+
  theme(text=element_text(size=14))

#ggsave('../Apps/Overleaf/merit paper/outputs/descriptive_quintile_percent_irish.pdf', plot = descriptive_plot, width=8, height=6)
ggsave('./replication_file/_5_outputs/figures/figure_a2.png', plot = descriptive_plot, width=8, height=6)

detach("package:Hmisc", unload = T)

